package cn.rkylin.oms.item.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.item.adapter.ItemAdapter;
import cn.rkylin.oms.item.adapter.ItemAdapterFactory;

@Controller
@RequestMapping("/itemTestManager")
public class ItemControllerTest extends ApolloController {

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * http://localhost:8080/OMSJobExecutor/itemTestManager/getItemAll.action
     * 
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getItemAll")
    public Map getItemAll() throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        String ShopId = "008480ccb1d64b30b843023fe5b13d3a";
        ItemAdapter adapter = ItemAdapterFactory.getItemAdapter(ShopId);
        if (adapter == null) {
            resultMap.put("success", "n");
            resultMap.put("msg", "adapter is null");
            return resultMap;
        }

        int r = adapter.downloadAll(ShopId);
        resultMap.put("success", "y");
        resultMap.put("msg", r);
        return resultMap;
    }

    /**
     * http://localhost:8080/OMSJobExecutor/itemTestManager/getItemByModifyTime.action
     * 
     * 
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getItemByModifyTime")
    public Map getItemByModifyTime() throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        String ShopId = "2232af4c1f0947f4b655e065eb4cae9b";
        ItemAdapter adapter = ItemAdapterFactory.getItemAdapter(ShopId);

        if (adapter == null) {
            resultMap.put("success", "n");
            resultMap.put("msg", "adapter is null");
            return resultMap;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        
        Calendar startTime = Calendar.getInstance();  
        startTime.add(Calendar.DATE, -1);
        Calendar endTime = Calendar.getInstance();

        paramMap.put("startTime", simpleDateFormat.format(startTime.getTime()));
        paramMap.put("endTime", simpleDateFormat.format(endTime.getTime()));

        int r = adapter.downloadByModifyTime(ShopId, paramMap);
        resultMap.put("success", "y");
        resultMap.put("msg", r);
        return resultMap;
    }

    /**
     * http://localhost:8081/OMSJobExecutor/itemTestManager/getItemByNumIid.action
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getItemByNumIid")
    public Map getItemByNumIid() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        String ShopId = "dc04d824c9534e32979e7b498fdc14a9";

        ItemAdapter adapter = ItemAdapterFactory.getItemAdapter(ShopId);
        if (adapter == null) {
            resultMap.put("success", "n");
            resultMap.put("msg", "adapter is null");
            return resultMap;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("itemCode", "551629923560");
        int r = adapter.downloadByItemCode(ShopId, paramMap);
        resultMap.put("success", "y");
        resultMap.put("msg", r);
        return resultMap;
    }

}
